<?php
class TransToPinyin{
    /*
    *将汉字字符串首转化成拼音首字母
    *若不是汉字则原样返回
    */
    static function firstLetter($str){
        //加载汉字拼音对应表
        require('data.php');

        //目标字符串
        $STR = '';
        for ($i = 0 , $k = mb_strlen($str,'UTF8'); $i < $k ; $i++) { 
            $word = mb_substr($str, $i, 1, 'utf8');
            $flag = false;
            foreach ($data as $key => $value) {
                if (mb_strpos($value, $word, 0, 'utf8')!==false) {
                    $STR .= substr($key, 0, 1);
                    $flag = true;
                    break;
                }
            }
            if (!$flag) {
                $STR .= $word;
            }
        }
        return $STR;
    }
    /*
    *将汉字字符串首转化成拼音组成字符串
    */
    static function hollLetter($str){
        //加载汉字拼音对应表
        require('data.php');
        //目标字符串
        $STR = '';
        for ($i = 0 , $k = mb_strlen($str,'UTF8'); $i < $k ; $i++) { 
            $word = mb_substr($str, $i, 1, 'utf8');
            $flag = false;
            foreach ($data as $key => $value) {
                if (mb_strpos($value, $word, 0, 'utf8')!==false) {
                    $STR .= $key;
                    $flag = true;
                    break;
                }
            }
            if (!$flag) {
                $STR .= $word;
            }
        }
        return $STR;
    }
}